package com.seal.yoyoshop.web;

import com.seal.yoyoshop.bin.Order;
import com.seal.yoyoshop.bin.Type;
import com.seal.yoyoshop.service.OrderService;
import com.seal.yoyoshop.service.TypeService;
import com.seal.yoyoshop.service.impl.OrderServiceImpl;
import com.seal.yoyoshop.service.impl.TypeServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "PayServlet", value = "/index/pay")
public class PayServlet extends HttpServlet {

    private OrderService orderService = new OrderServiceImpl();
    private TypeService typeService = new TypeServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        获取前端参数
        request.setCharacterEncoding("UTF-8");
        String orderIdstr = request.getParameter("id");
        System.out.println("id:"+orderIdstr);
        int orderId = Integer.parseInt(orderIdstr);
        String paytypestr = request.getParameter("paytype");
        int paytype = Integer.parseInt(paytypestr);
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");

//        根据订单id查订单
        Order order = orderService.getOrderById(orderId);

        order.setStatus(2);
        order.setPaytype(paytype);
        order.setName(name);
        order.setPhone(phone);
        order.setAddress(address);
//        更新order表信息
        boolean isSuccess = orderService.updateOrder(order);
        if(isSuccess) {
            request.setAttribute("msg","支付成功");

            List<Type> typeList = typeService.getTypeList();
            request.setAttribute("typeList", typeList);

            request.getRequestDispatcher("/index/payok.jsp").forward(request, response);
        }else {
            request.setAttribute("msg","支付失败");
            List<Type> typeList = typeService.getTypeList();
            request.setAttribute("typeList", typeList);

            request.getRequestDispatcher("/index/payok.jsp").forward(request, response);
        }
    }
}
